package java2_chatroom;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 聊天室单线程版本客户端
 * @author yuisama
 * @date 2018/8/31 19:02
 */
public class SingleThreadClient {
    public static void main(String[] args) {
        try {
            // 建立客户端Socket并绑定服务器
            Socket client = new Socket("127.0.0.1",6666);
            // 获取输入流，读取服务器发来的信息
            Scanner in = new Scanner(client.getInputStream());
            if (in.hasNextLine()) {
                System.out.println("服务器发来的信息为: "+in.nextLine());
            }
            // 获取输出流，向服务器发送信息
            PrintStream out = new PrintStream(client.getOutputStream(),
                    true,"UTF-8");
            out.println("Hi,I am Client!");
            in.close();
            out.close();
            client.close();
        } catch (IOException e) {
            System.err.println("客户端通信出现异常，错误为 "+e);
        }
    }
}
